package com.boe.csb.core.common.utils;

import com.boe.csb.core.common.exception.CSBErrorCode;
import com.boe.csb.core.common.exception.CSBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by tianxiang.luo on 16/11/24.
 */
public class PicUploadUtil {

    private static Logger logger = LoggerFactory.getLogger(PicUploadUtil.class);


    public static String uploadPic(CommonsMultipartFile file){
        String uploadFileName = MyRandomUtils.randomUUID() + "_" + file.getOriginalFilename();
        String picUrl = OssTool.DEFAULT_BUCKET+"."+OssTool.ENDPOINT+"/"+uploadFileName+file.getContentType()+"?"+"x-oss-process=image";
        try {
            InputStream inputStream = file.getInputStream();
            OssTool.uploadToOss(OssTool.DEFAULT_BUCKET,uploadFileName,inputStream);
        } catch (IOException e) {
            String errInfo = file.getOriginalFilename()+file.getContentType()+"图片上传失败!";
            logger.error(errInfo,e);
            throw new CSBException(CSBErrorCode.BASIC_ERROR,errInfo,e);
        }
        return picUrl;
    }

}
